package it.blueocean.acanto.taglibrary.jsp.taglib.tags.dataset.filter;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.exceptions.ComponentStateBeanNotFoundException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DatasetFilterConditionTag extends TagSupport
{
	private static final long serialVersionUID = 1L;

	private final static Logger logger_ = LoggerFactory.getLogger(DatasetFilterConditionTag.class);

	private String fieldName = "";

	private String operator = "";

	private Object value = null;

	private boolean notModeOn = false;

	private boolean caseInsensitive = true;

	private boolean emptyAsNull = true;

	public int doStartTag() throws JspException
	{
		int i = 0;
		if(this.value != null)
			if(this.value == null)
			{
				i = 0;
				logger_.debug("--->field name : " + this.fieldName + " value == null");
			}
			else if(this.value instanceof String)
			{
				if(this.value.toString().equals(""))
				{
					i = 0;
					logger_.debug("--->field name : " + this.fieldName + " value(string).equals()");
				}
				else
				{
					i = 1;
					logger_.debug("--->field name : " + this.fieldName + " not value(string).equals() "
							+ this.value.toString());
				}
			}
			else if(this.value != null)
			{
				i = 1;
				logger_.debug("--->field name : " + this.fieldName + " value != null " + this.value);
			}
		
		logger_.debug("fieldName" + this.fieldName + " - operator " + this.operator + " - value" + this.value);
		
		if(i != 0)
		{
			FieldConditionDatasetFilter localFieldConditionDatasetFilter = new FieldConditionDatasetFilter(
					this.fieldName, this.operator, this.value);
			localFieldConditionDatasetFilter.setNotModeOn(this.notModeOn);
			localFieldConditionDatasetFilter.setEmptyAsNull(this.emptyAsNull);
			DatasetFilterTag localDatasetFilterTag = (DatasetFilterTag) findAncestorWithClass(this,
					DatasetFilterTag.class);
			if(localDatasetFilterTag == null)
				logger_.error("DatasetFilterConditionTag can only be used inside a DatasetFilterTag");
			try
			{
				localDatasetFilterTag.addFilterCondition(localFieldConditionDatasetFilter);
			}
			catch (ComponentStateBeanNotFoundException localComponentStateBeanNotFoundException)
			{
				logger_.error(localComponentStateBeanNotFoundException.getMessage());
			}
		}
		return 6;
	}

	public boolean isCaseInsensitive()
	{
		return this.caseInsensitive;
	}

	public void setCaseInsensitive(boolean paramBoolean)
	{
		this.caseInsensitive = paramBoolean;
	}

	public boolean isEmptyAsNull()
	{
		return this.emptyAsNull;
	}

	public void setEmptyAsNull(boolean paramBoolean)
	{
		this.emptyAsNull = paramBoolean;
	}

	public String getFieldName()
	{
		return this.fieldName;
	}

	public void setFieldName(String paramString)
	{
		this.fieldName = paramString;
	}

	public boolean isNotModeOn()
	{
		return this.notModeOn;
	}

	public void setNotModeOn(boolean paramBoolean)
	{
		this.notModeOn = paramBoolean;
	}

	public String getOperator()
	{
		return this.operator;
	}

	public void setOperator(String paramString)
	{
		this.operator = paramString;
	}

	public Object getValue()
	{
		return this.value;
	}

	public void setValue(Object paramObject)
	{
		this.value = paramObject;
	}

}
